const Controller = require('egg').Controller;
const Error = require('../../class/error');
class FolderController extends Controller {
  //增加文件夹
  async addJson() {
    const ctx = this.ctx;
    try {
      const query = ctx.query;
      const userInfo = ctx.session.userInfo;
      const folder = await this.service.folder.create({
        name: query.name,
        parent_id: +query.parent_id,
        grand_parent_id: +query.grand_parent_id,
        create_time: new Date(),
        modify_time: new Date(),
        create_username: userInfo.real_name,
        create_userid: userInfo.user_id,
      });
      ctx.body = folder;
    // await ctx.redirect('/admin/resource/list');
    } catch (e) {
      ctx.body = "出错";
    }
  }

  /**
   * @description 删除文件夹
   * @author xiezuobing[948466]<435321508@qq.com>
   * @memberof FolderController
   */
  async clearJson() {
    const ctx = this.ctx;
    try {
      await this.service.folder.update({
        id: ctx.query.folderId,
        delete_flag: 1,
      });
      ctx.body = Error.SUCCESS;
    } catch (e) {
      ctx.body = Error.UNKOWN;
    }
  }

  /**
   * @description 编辑
   * @author xiezuobing[948466]<435321508@qq.com>
   * @memberof FolderController
   */
  async editJson() {
    const ctx = this.ctx;
    try {
      let params = {
        id: ctx.query.folderId,
        name: ctx.query.name,
        modify_time: new Date()
      }
      await this.service.folder.update(params);
      ctx.body = Error.SUCCESS;
    } catch (e) {
      ctx.body = Error.UNKOWN;
    }
  }
}

module.exports = FolderController;